package com.zhongshengnetwork.rightbe.common;

/**
 * Created by GordonLjx on 2016/9/27.
 */
import com.zhongshengnetwork.rightbe.Constant.Constant;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    private static SimpleDateFormat sf = null;

    /* 获取系统时间 格式为："yyyy/MM/dd " */
    public static String getCurrentDate() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }

    /* 时间戳转换成字符窜 */
    public static String getDateToString(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sf.format(d);
    }

    /* 时间戳转换成字符窜 */
    public static String getDateToMsString(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String str = sf.format(d);
        str += ".";
        long ms = time%1000;
        if (ms < 10){
            str += ("00"+ms);
        }else if (ms < 100){
            str += ("0"+ms);
        }else {
            str += ms;
        }
        return str;
    }

    /* 将字符串转为时间戳 */
    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String parseDate(long create){
        try {
            String ret = "";
            Calendar now = Calendar.getInstance();
            long ms  = 1000*(now.get(Calendar.HOUR_OF_DAY)*3600+now.get(Calendar.MINUTE)*60+now.get(Calendar.SECOND));//毫秒数
            long ms_now = now.getTimeInMillis();
            if(ms_now-create<ms){
                ret = "今天";
            }else if(ms_now-create<(ms+24*3600*1000)){
                ret = "昨天";
            }else if(ms_now-create<(ms+24*3600*1000*2)){
                ret = "前天";
            }else{
                ret= "更早";
            }
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //聊天时间
    public static String getChatDate(long date) {//date 是存储的时间戳

        /*
        long nowTime = System.currentTimeMillis();
        String dateStr = DateUtil.parseDate(date);
        if (dateStr == null){
            Date d = new Date(date);
            sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            String str = sf.format(d);
            return str;
        }else if (dateStr.equals("今天")){
            Date d = new Date(date);
            sf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
            String str = sf.format(d);
            return str;
        }else if (dateStr.equals("昨天") || dateStr.equals("前天")){//昨天的
            Date d = new Date(date);
            sf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
            String str = dateStr+" "+sf.format(d);
            return str;
        }else {
            Date d = new Date(date);
            sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            String str = sf.format(d);
            Calendar calendar=Calendar.getInstance();  //获取当前时间，作为图标的名字
            String year=calendar.get(Calendar.YEAR)+"";
            if (str.substring(0,4).equals(year)){
                //今年的
                sf = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault());
                str = sf.format(d);
            }
            return str;
        }
        */

        StringBuffer sb = new StringBuffer();
        long t = date;
        long time = System.currentTimeMillis() - t;
        long mill = (long) Math.ceil(time /1000);//秒前
        long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前
        long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时
        long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前
        if (day - 1 > 0) {
            //sb.append(day + "天");
            Date d = new Date(date);
            sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            String str = sf.format(d);
            Calendar calendar=Calendar.getInstance();  //获取当前时间，作为图标的名字
            String year=calendar.get(Calendar.YEAR)+"";
            if (str.substring(0,4).equals(year)){
                //今年的
                sf = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault());
                str = sf.format(d);
            }
            return str;
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            sb.append("前");
        }
        return sb.toString();

    }

    /**
     * 将时间戳转为代表"距现在多久之前"的字符串
     * @param timeStr	时间戳
     * @return
     */
    public static String getStandardDate(String timeStr) {

        StringBuffer sb = new StringBuffer();

        long t = Long.parseLong(timeStr);
        long time = System.currentTimeMillis() - (t*1000);
        long mill = (long) Math.ceil(time /1000);//秒前

        long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前

        long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时

        long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前

        if (day - 1 > 0) {
            sb.append(day + "天");
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            sb.append("前");
        }
        return sb.toString();
    }

    //1天内的时间显示
    public static String dayTimeString(long t){
        StringBuffer sb = new StringBuffer();
        long time = System.currentTimeMillis() - t;
        long mill = (long) Math.ceil(time /1000);//秒前

        long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前

        long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时

        long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前

        if (day - 1 > 0) {
            sb.append(day + "天");
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            sb.append("前");
        }
        return sb.toString();
    }

    public static String getDateStr(Date date,String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static String timeStamp2Date(long time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    public static String date2TimeStamp(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
//        String TIME = DateUtil.getDateToString(1445875200000l);
//        System.out.println("TIME==" + TIME);
//
//        String CURRENTTIME = DateUtil.getCurrentDate();
//        System.out.println("CURRENTtime" + CURRENTTIME);
//        long CHUO = DateUtil.getStringToDate(CURRENTTIME);
//        System.out.println(CHUO);

        System.out.println("时间转时间戳:"+date2TimeStamp("2020-09-03 08:55", "yyyy-MM-dd HH:mm"));

    }

}
